import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { ElevatorUsedInspection } from "./bean/elevator-used-inspection.bean";
import { SafeControlPage } from "../safe-control/safe-control";
/**
 * 单位安全管理检查
 */
@Component({
  templateUrl: "elevator-used-inspection.html"
})
export class ElevatorUsedInspectionPage {
  private elevatorUsedInspection: ElevatorUsedInspection = new ElevatorUsedInspection();
  isDetail = false;
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private httpService: HttpService,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private storage: Storage
  ) {
    if (this.navParams.get("id")) {
      this.isDetail = true;

      this.httpService
        .queryelevatorUsed(this.navParams.get("id"))
        .subscribe(data => {
          if (data.status == "200") {
            this.elevatorUsedInspection = data.data;
          }
        });
    }
  }

  submit() {
    for (let key in this.elevatorUsedInspection) {
      if (key != "suddenStopSwitch") {
        if (key != "id" && this.elevatorUsedInspection[key] == null) {
          const alert = this.alertCtrl.create({
            subTitle: "检查项填写不完整",
            buttons: ["确定"]
          });
          alert.present();
          return;
        }
      }
    }
    this.httpService
      .addEleUsedInspect(this.elevatorUsedInspection)
      .subscribe(data1 => {
        if (data1.status == 200) {
          this.navCtrl.push(SafeControlPage, {
            checkTimeFrom: this.navParams.get("checkTimeFrom"),
            usedControlId: this.navParams.get("usedControlId"),
            elevatorControlId: data1.data.elevatorControlId
          });
        } else {
          const alert = this.alertCtrl.create({
            subTitle: "信息保存失败",
            buttons: ["确定"]
          });
          alert.present();
        }
      });
  }
}
